<?php

declare(strict_types=1);

namespace App\Request\Cooperative;

use App\Constants\Model\CooperativeUser;
use Hyperf\Validation\Request\FormRequest;
use Hyperf\Validation\Rule;

class CooperativeUserLoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'phone' => 'required',
            'password' => Rule::requiredIf(function () {
                return $this->input('type') == CooperativeUser::LOGIN_WITH_PASSWORD;
            }),
            'captcha_key' => Rule::requiredIf(function () {
                return $this->input('type') == CooperativeUser::LOGIN_WITH_PASSWORD;
            }),
            'captcha_code' => Rule::requiredIf(function () {
                return $this->input('type') == CooperativeUser::LOGIN_WITH_PASSWORD;
            }),
            'code' => Rule::requiredIf(function () {
                return $this->input('type') == CooperativeUser::LOGIN_WITH_CODE;
            }),
            'type' => ['required', Rule::in(CooperativeUser::LOGIN_TYPES)]
        ];
    }
}
